package java9_23zy;

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Test_3 {
    public static void main(String[] args) {
 modif("2949","100");

  add(new Shop("ture","合肥","001","西兰花","6","100","斤"));
    }
    //修改
    private static void modif(String itemno,String stockcount)  {
        SAXReader s=new SAXReader();
        Document d=null;
        try {
            d=s.read(new File("D:\\aaa\\src\\java9_23zy\\products.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        Element node = (Element)d.selectSingleNode("/shop/product[itemno=" + itemno + "]");
        if (node==null){
            System.out.println("没有找到该商品");
            return;
        }
        node.selectSingleNode("stockcount").setText(stockcount);
        OutputFormat o=OutputFormat.createPrettyPrint();
        o.setEncoding("utf-8");
        try {
            XMLWriter x = new XMLWriter(new FileOutputStream("D:\\aaa\\src\\java9_23zy\\products.xml" ),o);
            x.write(d);
            x.close();
            System.out.println("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//添加新商品
    private static void add(Shop shop){
SAXReader s=new SAXReader();
Document d=null;
        try {
            d= s.read(new File("D:\\aaa\\src\\java9_23zy\\products.xml"));
            Element r1 = d.getRootElement();//获取更目录
            ArrayList<Shop> products = new ArrayList<>();
            List<Element> l = r1.selectNodes("product");
            for (Element e : l) {
                String itemno= e.selectSingleNode("itemno").getText();
                String name= e.selectSingleNode("name").getText();
                String price= e.selectSingleNode("price").getText();
                String stockcount= e.selectSingleNode("stockcount").getText();
                String unit= e.selectSingleNode("unit").getText();
                Shop s1=new Shop();
                s1.setItemno(itemno);
                s1.setName(name);
                s1.setPrice(price);
                s1.setStockcount(stockcount);
                s1.setUnit(unit);
                products.add(s1);
            }
            for (Shop product : products) {
                if (product.getItemno().equals(shop.getItemno())){
                    System.out.println("商品信息重复");
                    return;
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
            d = DocumentHelper.createDocument();
            d.addElement("shop");
        }
        Element e = d.getRootElement();
        Element r = e.addElement("product");
      r.addAttribute("discount",shop.getDiscount());
      r.addAttribute("factoryaddr",shop.getFactoryaddr());
        Element e1 = r.addElement("itemno");
        e1.setText(shop.getItemno());
        Element n = r.addElement("name");
        n.setText(shop.getName());
        Element p = r.addElement("price");
        p.setText(shop.getPrice());
        Element s2 = r.addElement("stockcount");
        s2.setText(shop.getStockcount());
        Element e3 = r.addElement("unit");
        e3.setText(shop.getUnit());

        try {
            OutputFormat o=OutputFormat.createPrettyPrint();
            o.setEncoding("utf-8");
            XMLWriter x=new XMLWriter(new FileOutputStream("D:\\aaa\\src\\java9_23zy\\products.xml"),o);
            x.write(d);
            x.close();
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
}
